/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package formatconversion_model;

import magic_model.Card;
import magic_model.Deck;
import magic_model.MalformedFileException;

/**
 *
 * @author Tobias
 */
public class Basic_Parser implements MagicFormatParser {

    private int count=0;
    
  @Override
    public String toString(){
        return "Basic";
    }

    public Deck importFile(String[] lines) {
        //ArrayList<String> tempLines = new ArrayList<String>();
        Deck deck = new Deck();
        for(int i=0;i<lines.length;i++){
            parseLine(lines[i],deck);
        }
        deck.sort();
        return deck;
    }
    
    private void parseLine(String line,Deck deck) {
     // System.out.println("parsing: "+line);
        line = line.trim();
      
        if (line.equals(""))return;
        if(line.contains("//")){
           /* if(line.contains("deckname")){
                deck.setName(line);
            }*/
                return;
            
        }
        if(!line.contains (" ")) throw new MalformedFileException("File is not in # Name - Format. "+line);
        
            String name =  line.substring(line.indexOf(" "));
            name=name.trim();
            int quantity = Integer.parseInt(line.substring(0,line.indexOf(" ")));
        
          
            
            boolean isMain=true;
            if (count>=60){
                isMain=false;
            }
            count+=quantity;
        deck.addCard(new Card(quantity,name,isMain));
     
      

       
    }

    public String[] exportFile(Deck intern) {
        String[] content = new String[intern.getCards().size()];
        for(int i=0;i<intern.getCards().size();i++){
            Card c = intern.getCards().get(i);
            content[i]= c.getQuantity()+" "+c.getName();
        }
        return content;
    }

    public FileType getFileType() {
        return FileType.basic;
    }
    
}
